package main;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class PassageFilter {

	String input;

	String output;

	List topics;

	final static int totalSize = 1000;

	public PassageFilter() {

		initConfig();

		read();
		System.out.println("sort begin.............");
		Date begin = new Date();
		for (Iterator iter = topics.iterator(); iter.hasNext();) {
			Topic topic = (Topic) iter.next();
			List psgs = topic.getPassages();
			filtSamePassage(psgs);

		}
		System.out.println("sort end.............");
		Date end = new Date();
		write(topics);
		System.out.println("sort time:" + (end.getTime() - begin.getTime()));

	}

	private void write(List topics) {
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(output));
			for (Iterator iter = topics.iterator(); iter.hasNext();) {
				Topic topic = (Topic) iter.next();
				int i = 0;
				for (Iterator itP = topic.getPassages().iterator(); itP.hasNext(); i++) {
					Passage psg = (Passage) itP.next();
					bw.write(psg.getOutString() + "\r\n");
					System.out.println(psg);
				}

			}
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void filtSamePassage(List psgs) {
		List removeIndex = new ArrayList();
		for (int i = 0; i < psgs.size() - 1; i++) {
			Passage psg1 = (Passage) psgs.get(i);
			Passage psg2 = (Passage) psgs.get(i + 1);

			if (isFullSame(psg1,psg2)) {
				removeIndex.add(new Integer(i + 1));
			}
		}
		// remove psgs
		if (removeIndex.size() > 0) {
			for (int i = psgs.size(); i >= 0; i--) {
				for (int j = 0; j < removeIndex.size(); j++) {
					if (i == ((Integer) removeIndex.get(j)).intValue()) {
						psgs.remove(i);
					}
				}
			}
			filtSamePassage(psgs);
		}
	}

	private boolean isSame(Passage p1,Passage p2){
		if(p1.getDocumentID().equals(p2.getDocumentID())){
			return true;
		}
		return false;
	}
	
	private boolean isFullSame(Passage p1, Passage p2) {
		if (p1.getDocumentID().equals(p2.getDocumentID()) && p1.getStart().intValue() == p2.getStart().intValue()
				&& p1.getOffset().intValue() == p2.getOffset().intValue()) {
			return true;
		}
		return false;
	}
	
	private void read() {
		topics = new ArrayList();
		System.out.println("read file begin.............");
		try {

			// readTopPassages();
			// readTermCount();

			readTermFormatted();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("read file end.............");

	}

	/**
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	private void readTermFormatted() throws FileNotFoundException, IOException {
		BufferedReader fmt_file = new BufferedReader(new FileReader(input));
		for (String tmp = fmt_file.readLine(); tmp != null; tmp = fmt_file.readLine()) {
			String[] tmpArr = tmp.split(" ");
			if (tmpArr.length == 7) {
				Topic topic = getTopic(tmpArr[0]);
				if (topic == null) {
					topic = new Topic();
					topic.setId(tmpArr[0]);
					topics.add(topic);
				}

				Passage psg = new Passage();
				psg.setDocumentID(tmpArr[1]);
				psg.setStart(new Integer(tmpArr[4]));
				psg.setOffset(new Integer(tmpArr[5]));
				psg.setOutString(tmp);
				topic.addPassage(psg);
			}
		}
		fmt_file.close();
	}

	/**
	 * @param id
	 * @return
	 */
	private Topic getTopic(String id) {
		for (Iterator iter = topics.iterator(); iter.hasNext();) {
			Topic thetopic = (Topic) iter.next();
			if (id.equals(thetopic.getId())) {
				return thetopic;
			}
		}
		return null;
	}

	/**
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	private void initConfig() {
		try {
			BufferedReader conf_file = new BufferedReader(new FileReader("config/passage_filter_config.properties"));
			for (String tmp = conf_file.readLine(); tmp != null; tmp = conf_file.readLine()) {
				String[] conf = tmp.split("=");
				if (conf[0].equals("input-file-name")) {
					this.input = conf[1];
				} else if (conf[0].equals("output-file-name")) {
					this.output = conf[1];
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		PassageFilter cs = new PassageFilter();

	}
}
